using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.IO;

class BuildProcessor : IPostprocessBuildWithReport
{
    public int callbackOrder { get { return 0; } }
    public void OnPostprocessBuild(BuildReport report)
    {
        Debug.LogFormat("BuildProcessor.OnPostprocessBuild for target {0} at path {1}",
            report.summary.platform, report.summary.outputPath);

        string originPath = report.summary.outputPath;
        string prePath = originPath.Substring(0, originPath.Length - 4);
        string newPath = string.Format("{0}{1}.apk", prePath, UnityEditor.PlayerSettings.bundleVersion);
        if (File.Exists(newPath))
        {
            File.Delete(newPath);
        }
        File.Copy(originPath, newPath);
        Debug.LogFormat("CopyFile from {0} to {1}", originPath, newPath);

        Debug.LogFormat("版本{0}打包成功", UnityEditor.PlayerSettings.bundleVersion);
    }

    [MenuItem("Build/Build Android")]
    public static void BuildAndroid()
    {
        // 版本步进
        float version = float.Parse(UnityEditor.PlayerSettings.bundleVersion);
        if (m_IsStep)
        {
            UnityEditor.PlayerSettings.bundleVersion = ((version * 100 + 1) / 100f).ToString();
        }
        else
        {
            UnityEditor.PlayerSettings.bundleVersion = version.ToString();
        }

        // 打包
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
        buildPlayerOptions.scenes = new[] {
            "Assets/Scenes/Login.unity",
            "Assets/Scenes/SelectMode.unity",
            "Assets/Scenes/GamePlay.unity" };
        string pathName = string.Format("Builds/Android/MineSweeper.apk");
        buildPlayerOptions.locationPathName = pathName;
        buildPlayerOptions.target = BuildTarget.Android;
        buildPlayerOptions.options = BuildOptions.None;

        BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
        BuildSummary summary = report.summary;

        if (summary.result == BuildResult.Succeeded)
        {
            Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
        }

        if (summary.result == BuildResult.Failed)
        {
            Debug.Log("Build failed");
        }
    }


    [MenuItem("Build/Check Version")]
    public static void CheckVersion()
    {
        Debug.LogFormat("bundleVersion:{0}", UnityEditor.PlayerSettings.bundleVersion);
        Debug.LogFormat("bundleVersion:{0}", UnityEditor.PlayerSettings.applicationIdentifier);
        Debug.LogFormat("IsStep:{0}", m_IsStep);
    }

    [MenuItem("Build/Use Step")]
    public static void UseStep()
    {
        m_IsStep = true;
    }

    [MenuItem("Build/Use Step", true)]
    public static bool UseStepValidate()
    {
        return m_IsStep == false;
    }

    [MenuItem("Build/No Step")]
    public static void NoStep()
    {
        m_IsStep = false;
    }

    [MenuItem("Build/No Step", true)]
    public static bool NoStepValidate()
    {
        return m_IsStep == true;
    }

    private static bool m_IsStep;
}